/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "json_error.h"
#include <unordered_map>
#include "yyjson.h"

using namespace json_error;

std::unordered_map<size_t, Napi_Error> NapiErrorMessages = {
    {napi_invalid_arg, {"napi_invalid_arg", "Invalid argument"}},
    {napi_object_expected, {"napi_object_expected", ""}},
    {napi_string_expected, {"napi_string_expected", ""}},
    {napi_name_expected, {"napi_name_expected",""}},
    {napi_function_expected, {"napi_function_expected", ""}},
    {napi_number_expected, {"napi_number_expected", ""}},
    {napi_boolean_expected, {"napi_boolean_expected", ""}},
    {napi_array_expected, {"napi_array_expected", ""}},
    {napi_generic_failure, {"napi_generic_failure", }},
    {napi_pending_exception, {"napi_pending_exception", ""}},
    {napi_cancelled, {"napi_cancelled", ""}},
    {napi_escape_called_twice, {"napi_escape_called_twice",""}},
    {napi_handle_scope_mismatch, {"napi_handle_scope_mismatch", ""}},
    {napi_callback_scope_mismatch, {"napi_callback_scope_mismatch", ""}},
    {napi_queue_full, {"napi_queue_full", ""}},
    {napi_closing, {"napi_closing", ""}},
    {napi_bigint_expected, {"napi_bigint_expected", ""}},
    {napi_date_expected, {"napi_date_expected", ""}},
    {napi_arraybuffer_expected, {"napi_arraybuffer_expected", ""}},
    {napi_detachable_arraybuffer_expected, {"napi_detachable_arraybuffer_expected", ""}},
    {napi_would_deadlock, {"napi_would_deadlock",""}},
    {napi_create_ark_runtime_too_many_envs, {"napi_create_ark_runtime_too_many_envs", ""}},
    {napi_create_ark_runtime_only_one_env_per_thread, {"napi_create_ark_runtime_only_one_env_per_thread", ""}},
    {napi_destroy_ark_runtime_env_not_exist, {"napi_destroy_ark_runtime_env_not_mnexist", ""}},
    {napi_param_number_error, {"napi_param_number_error", ""}},
    {napi_param_type_error, {"napi_param_type_error", ""}},
};

std::unordered_map<size_t, Napi_Error> YYJSONErrorMessages = {
    {YYJSON_READ_ERROR_MORE, {"YYJSON_READ_ERROR_MORE", "Read more error"}},
    {YYJSON_CREATE_STATE_FAILED, {"YYJSON_CREATE_STATE_FAILED", "Create yyjson_incr_state failed"}},
    {YYJSON_CREATE_DOC_FAILED, {"YYJSON_CREATE_DOC_FAILED", "Create yyjson_doc failed"}},
    {YYJSON_MUT_WRITE_ERROR, {"YYJSON_MUT_WRITE_ERROR", "yyjson doc mut write error"}},
};

void json_error::handleNapiError(napi_env env, size_t err) {
    if (err != napi_ok) {
        napi_throw_error(env, NapiErrorMessages[err].code, NapiErrorMessages[err].msg);
    }
}

void json_error::handleYYJSONError(napi_env env, size_t err) {
    napi_throw_error(env, YYJSONErrorMessages[err].code, YYJSONErrorMessages[err].msg);
}